/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project2;

/**
 *
 * @author deylo
 */
public class GenTreeNode<T extends Comparable<T>> {

    GenTreeNode parentNode;
    GenTreeNode rightNode;
    T data;
    GenTreeNode leftNode;

    public GenTreeNode() {
        leftNode = rightNode = null;
        this.parentNode = this;
    }
//
//    public GenTreeNode(T nodeData) {
//        try {
//            data = nodeData.newInstance();
//            leftNode = rightNode = null;
//            this.parentNode = this;
//        } catch (Exception ex) {
//        }
//    }

    public GenTreeNode(T nodeData, GenTreeNode parentNode) {
        data = nodeData;
        leftNode = rightNode = null;
        this.parentNode = parentNode;
    }

    public void insert(T insertValue, GenTreeNode parentNode) {
        // insert in left subtree
        if (insertValue.compareTo(data) == -1) {
            // insert new TreeNode
            if (leftNode == null) {
                this.parentNode = parentNode;
                leftNode = new GenTreeNode(insertValue, this);
            } else // continue traversing left subtree via recursion
            {
                leftNode.insert(insertValue, this);
            }
        } else if (insertValue.compareTo(data) == 1) // insert in right
        {
            if (rightNode == null) {
                this.parentNode = parentNode;
                rightNode = new GenTreeNode(insertValue, this);
            } else // continue traversing right subtree via recursion
            {
                rightNode.insert(insertValue, this);
            }
        }
    }
}
